package com.jvtd.eyl.ui.main.my;

import android.databinding.DataBindingUtil;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.jvtd.eventBus.EventCenter;
import com.jvtd.eyl.R;
import com.jvtd.eyl.base.BaseMvpFragment;
import com.jvtd.eyl.bean.binding.BrowseBean;
import com.jvtd.eyl.bean.eventBus.MainBarBean;
import com.jvtd.eyl.constants.EventCode;
import com.jvtd.eyl.databinding.FragmentMyBinding;
import com.jvtd.utils.SmartRefreshUtils;
import com.jvtd.utils.StringUtils;
import com.jvtd.utils.UiUtils;
import com.jvtd.widget.emptyView.JvtdEmptyView;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.inject.Inject;

/**
 * 我的
 *
 * 作者:chenlei
 * 时间:2018/11/12 3:17 PM
 */
public class MyFragment extends BaseMvpFragment implements MyMvpView, OnRefreshListener {
    @Inject
    MyPresenter<MyMvpView> mPresenter;
    private FragmentMyBinding mBinding;

    public static MyFragment newInstance() {
        return new MyFragment();
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    protected View getLayout(LayoutInflater inflater, @Nullable ViewGroup container) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_my, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onDestroyView() {
        mPresenter.onDetach();
        super.onDestroyView();
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        MainBarBean mainBarBean = new MainBarBean(true,false);
        EventBus.getDefault().post(new EventCenter<>(mainBarBean,EventCode.UPDATE_BAR));
    }

    @Override
    protected void initViewAndData() {
        mPresenter.onAttach(this);

        initHeaderView();

        initRefreshLayout();

        initRecyclerView();

        onRefresh(null);
    }

    private void initHeaderView() {
        mBinding.nameTextView.setText(mPresenter.getCurrentUser().getName());
        department(mPresenter.getCurrentUser().getDepartment());
//        addDisposable(RxView.clicks(mBinding.showAllBtn).subscribe(o -> toBrowseList()));
        UiUtils.setAutoHeight(mBinding.topView,UiUtils.windowWidth(mContext),375.f / 150.f);
    }

    //设置组织架构
    private void department(String department) {
        mBinding.textView.setText(getDepartment(department));
    }

    //组织架构获取
    public static String getDepartment(String paramValue) {
        String[] stringList = paramValue.split(",");
        List<String> strings = new ArrayList<>();
        for (String s : stringList) {
            String[] tempStr = s.split("=");
            if (tempStr.length > 1){
                strings.add(tempStr[1]);
            }else {
                strings.add(s.replace("ou=",""));
            }
        }
//        String regex = "([0-9\u4e00-\u9fa5]+)";
//        List<String> strings = new ArrayList<>();
//        Matcher matcher = Pattern.compile(regex).matcher(paramValue);
//        while (matcher.find()) {
//            strings.add(matcher.group(0));
//        }
//        Collections.reverse(strings);
        return StringUtils.list2String(strings, '-');
    }

    private void initRefreshLayout() {
        SmartRefreshUtils.initRefresh(mContext, mBinding.refreshLayout, this);
    }

    private void initRecyclerView(){
        mBinding.recyclerView.setLimit(true);
        mBinding.recyclerView.setRefreshLayout(mBinding.refreshLayout);
        mBinding.recyclerView.openEmptyView((view, state) -> {
            if (state == JvtdEmptyView.STATE_ERROR)
                onRefresh(null);
        });
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        mPresenter.getMyBrowseList();
    }

    @Override
    public void getMyBrowseListSuccess(List<BrowseBean> myBrowseBeans) {
        mBinding.recyclerView.setData(myBrowseBeans);
    }

    @Override
    public void getMyBrowseListFailed() {
        mBinding.recyclerView.firstLoadFailed();
    }
}
